﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Game
{
    /// <summary>
    /// 加载预制体服务
    /// </summary>
    public class LoadPrefabService : IGameAnyLoadPrefabListener
    {
        public static LoadPrefabService Instance { private set; get; } = new LoadPrefabService();

        private Contexts _contexts;

        private Transform _settleParent;
        private Transform _moveParent;

        public void Init(Contexts contexts,Transform gameController)
        {
            this._contexts = contexts;
            contexts.game.CreateEntity().AddGameAnyLoadPrefabListener(this);
            CreatSubParent(gameController);
        }

        private void CreatSubParent(Transform parent)
        {
            _settleParent = new GameObject("Settle").transform;
            _settleParent.SetParent(parent);
            _moveParent = new GameObject("Moveable").transform;
            _moveParent.SetParent(parent);

        }


        public void OnGameAnyLoadPrefab(GameEntity entity, string prefabPath)
        {
            Transform temp = null;

            if (entity.isGameMoveable)
            {
                temp = _moveParent;
            }
            else
            {
                temp = _settleParent;
            }

            GameObject go = Resources.Load<GameObject>(prefabPath);
            var view = Object.Instantiate(go, temp).GetComponent<IView>();
            view.Link(entity,_contexts.game);

        }
    }

}
